package com.debao.quartz.task;

import com.debao.common.constant.WechatConstants;
import com.debao.common.utils.StringUtils;
import com.debao.common.wechat.WechatUtils;
import com.debao.system.service.IConfigService;
import com.debao.system.service.ISysUserService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


@Component("wechat")
public class WechatTask {

    @Resource
    private IConfigService configService;

    @Resource
    private ISysUserService userService;

    /**
     * 更新token
     */
    public void updateToken() {
        //微信小程序appId
        String appId = configService.selectValueByKey(WechatConstants.APP_ID);
        //微信小程序secret
        String secret = configService.selectValueByKey(WechatConstants.APP_SECRET);

        //微信小程序token
        String token = WechatUtils.getToken(appId, secret);
        //微信公众号token
        if (token != null && !token.isEmpty()) {
            // 更新
            configService.updateValueByKey(WechatConstants.APP_TOKEN, token);
        }
    }

    /**
     * 更新token
     */
    public void updateAccountToken() {
        //微信公众号appId
        String accountAppId = configService.selectValueByKey(WechatConstants.APP_ACCOUNT_ID);
        //微信公众号secret
        String accountSecret = configService.selectValueByKey(WechatConstants.APP_ACCOUNT_SECRET);
        //微信公众号token
        String accountToken = WechatUtils.getAccountToken(accountAppId, accountSecret);
        if (accountToken != null && !accountToken.isEmpty()) {
            // 更新
            configService.updateValueByKey(WechatConstants.APP_ACCOUNT_TOKEN, accountToken);
        }
    }
}
